package com.zenithmind.document.pojo.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import jakarta.validation.constraints.Size;

/**
 * 文档收藏更新DTO
 * 遵循单一职责原则：专注于收藏更新数据传输
 * 遵循开闭原则：支持字段的扩展
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@Schema(description = "文档收藏更新DTO")
public class DocumentFavoriteUpdateDTO {

    @Schema(description = "收藏夹ID", example = "folder123456")
    private String folderId;

    @Schema(description = "收藏夹名称", example = "我的重要收藏")
    @Size(max = 100, message = "收藏夹名称不能超过100字符")
    private String folderName;

    @Schema(description = "收藏夹描述", example = "更新后的收藏夹描述")
    @Size(max = 500, message = "收藏夹描述不能超过500字符")
    private String folderDescription;

    @Schema(description = "收藏夹颜色", example = "#2196F3")
    private String folderColor;

    @Schema(description = "收藏夹图标", example = "fa-bookmark")
    private String folderIcon;

    @Schema(description = "标签", example = "重要,学习,参考,更新")
    @Size(max = 200, message = "标签不能超过200字符")
    private String tags;

    @Schema(description = "备注", example = "更新后的备注信息")
    @Size(max = 1000, message = "备注不能超过1000字符")
    private String remarks;

    @Schema(description = "是否公开", example = "false")
    private Boolean isPublic;

    @Schema(description = "排序权重", example = "200")
    private Integer sortWeight;

    @Schema(description = "状态", example = "ACTIVE", allowableValues = {"ACTIVE", "INACTIVE"})
    private String status;

    @Schema(description = "扩展属性", example = "{\"priority\": \"medium\"}")
    private String extendedProperties;

    @Schema(description = "更新原因", example = "调整收藏夹分类")
    private String updateReason;

    /**
     * 验证更新数据
     */
    public boolean isValid() {
        // 验证收藏夹名称长度
        if (folderName != null && folderName.length() > 100) return false;
        
        // 验证收藏夹描述长度
        if (folderDescription != null && folderDescription.length() > 500) return false;
        
        // 验证标签长度
        if (tags != null && tags.length() > 200) return false;
        
        // 验证备注长度
        if (remarks != null && remarks.length() > 1000) return false;
        
        return true;
    }

    /**
     * 是否有收藏夹更新
     */
    public boolean hasFolderUpdate() {
        return folderId != null || folderName != null || folderDescription != null ||
               folderColor != null || folderIcon != null;
    }

    /**
     * 是否有标签更新
     */
    public boolean hasTagsUpdate() {
        return tags != null;
    }

    /**
     * 是否有备注更新
     */
    public boolean hasRemarksUpdate() {
        return remarks != null;
    }

    /**
     * 是否有公开状态更新
     */
    public boolean hasPublicStatusUpdate() {
        return isPublic != null;
    }

    /**
     * 是否有排序权重更新
     */
    public boolean hasSortWeightUpdate() {
        return sortWeight != null;
    }

    /**
     * 是否有状态更新
     */
    public boolean hasStatusUpdate() {
        return status != null;
    }

    /**
     * 获取标签数组
     */
    public String[] getTagArray() {
        if (tags == null || tags.trim().isEmpty()) {
            return new String[0];
        }
        return tags.split(",");
    }

    /**
     * 获取更新摘要
     */
    public String getUpdateSummary() {
        StringBuilder summary = new StringBuilder();
        
        if (hasFolderUpdate()) {
            summary.append("收藏夹信息已更新; ");
        }
        
        if (hasTagsUpdate()) {
            summary.append("标签已更新; ");
        }
        
        if (hasRemarksUpdate()) {
            summary.append("备注已更新; ");
        }
        
        if (hasPublicStatusUpdate()) {
            summary.append("公开状态: ").append(isPublic ? "公开" : "私有").append("; ");
        }
        
        if (hasSortWeightUpdate()) {
            summary.append("排序权重: ").append(sortWeight).append("; ");
        }
        
        if (hasStatusUpdate()) {
            summary.append("状态: ").append(status).append("; ");
        }
        
        return summary.length() > 0 ? summary.toString() : "无更新";
    }

    /**
     * 创建收藏夹更新
     */
    public static DocumentFavoriteUpdateDTO createFolderUpdate(String folderId, String folderName, String reason) {
        DocumentFavoriteUpdateDTO dto = new DocumentFavoriteUpdateDTO();
        dto.setFolderId(folderId);
        dto.setFolderName(folderName);
        dto.setUpdateReason(reason);
        return dto;
    }

    /**
     * 创建标签更新
     */
    public static DocumentFavoriteUpdateDTO createTagsUpdate(String tags, String reason) {
        DocumentFavoriteUpdateDTO dto = new DocumentFavoriteUpdateDTO();
        dto.setTags(tags);
        dto.setUpdateReason(reason);
        return dto;
    }

    /**
     * 创建备注更新
     */
    public static DocumentFavoriteUpdateDTO createRemarksUpdate(String remarks, String reason) {
        DocumentFavoriteUpdateDTO dto = new DocumentFavoriteUpdateDTO();
        dto.setRemarks(remarks);
        dto.setUpdateReason(reason);
        return dto;
    }

    /**
     * 创建公开状态更新
     */
    public static DocumentFavoriteUpdateDTO createPublicStatusUpdate(Boolean isPublic, String reason) {
        DocumentFavoriteUpdateDTO dto = new DocumentFavoriteUpdateDTO();
        dto.setIsPublic(isPublic);
        dto.setUpdateReason(reason);
        return dto;
    }

    /**
     * 创建状态更新
     */
    public static DocumentFavoriteUpdateDTO createStatusUpdate(String status, String reason) {
        DocumentFavoriteUpdateDTO dto = new DocumentFavoriteUpdateDTO();
        dto.setStatus(status);
        dto.setUpdateReason(reason);
        return dto;
    }

    /**
     * 是否为重要更新
     */
    public boolean isSignificantUpdate() {
        return hasFolderUpdate() || hasPublicStatusUpdate() || hasStatusUpdate();
    }

    /**
     * 计算更新重要性分数
     */
    public double calculateUpdateImportance() {
        double score = 0.0;
        
        // 收藏夹更新权重 30%
        if (hasFolderUpdate()) score += 0.3;
        
        // 公开状态更新权重 25%
        if (hasPublicStatusUpdate()) score += 0.25;
        
        // 状态更新权重 20%
        if (hasStatusUpdate()) score += 0.2;
        
        // 标签更新权重 15%
        if (hasTagsUpdate()) score += 0.15;
        
        // 备注更新权重 10%
        if (hasRemarksUpdate()) score += 0.1;
        
        return Math.min(score, 1.0);
    }
}
